home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / authstuff / omakeIPFP.c.z / omakeIPFP.c
C/C++ Source or Header  |  1998-01-21  |  8KB  |  362 lines

  1. /*
  2.  * makeIPFP - make fast DES IP and FP tables
  3.  *
  4.  * This is an older version which generated tables half the size of
  5.  * the current version, but which took about double the CPU time to
  6.  * compute permutations from these tables.  Since the CPU spent on the
  7.  * permutations is small compared to the CPU spent in the cipher code,
  8.  * I may go back to the smaller tables to save the space some day.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13.  
  14. #include "ntp_stdlib.h"
  15.  
  16. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  17.  
  18. u_long IPL[8][16];
  19. u_long FPL[8][16];
  20.  
  21. char *progname;
  22. int debug;
  23.  
  24. static    void    perm    P((u_char *, u_char *, u_long *, u_long *));
  25. static    void    doit    P((void));
  26.  
  27. /*
  28.  * main - parse arguments and handle options
  29.  */
  30. void
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     int c;
  36.     int errflg = 0;
  37.     extern int ntp_optind;
  38.     extern char *ntp_optarg;
  39.  
  40.     progname = argv[0];
  41.     while ((c = ntp_getopt(argc, argv, "d")) != EOF)
  42.         switch (c) {
  43.         case 'd':
  44.             ++debug;
  45.             break;
  46.         default:
  47.             errflg++;
  48.             break;
  49.         }
  50.     if (errflg) {
  51.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  52.         exit(2);
  53.     }
  54.     doit();
  55.     exit(0);
  56. }
  57.  
  58.  
  59. /*
  60.  * Initial permutation table
  61.  */
  62. u_char IP[64] = {
  63.     58, 50, 42, 34, 26, 18, 10,  2,
  64.     60, 52, 44, 36, 28, 20, 12,  4,
  65.     62, 54, 46, 38, 30, 22, 14,  6,
  66.     64, 56, 48, 40, 32, 24, 16,  8,
  67.     57, 49, 41, 33, 25, 17,  9,  1,
  68.     59, 51, 43, 35, 27, 19, 11,  3,
  69.     61, 53, 45, 37, 29, 21, 13,  5,
  70.     63, 55, 47, 39, 31, 23, 15,  7
  71. };
  72.  
  73. /*
  74.  * Inverse initial permutation table
  75.  */
  76. u_char FP[64] = {
  77.     40,  8, 48, 16, 56, 24, 64, 32,
  78.     39,  7, 47, 15, 55, 23, 63, 31,
  79.     38,  6, 46, 14, 54, 22, 62, 30,
  80.     37,  5, 45, 13, 53, 21, 61, 29,
  81.     36,  4, 44, 12, 52, 20, 60, 28,
  82.     35,  3, 43, 11, 51, 19, 59, 27,
  83.     34,  2, 42, 10, 50, 18, 58, 26,
  84.     33,  1, 41,  9, 49, 17, 57, 25
  85. };
  86.  
  87.  
  88. /*
  89.  * Bit order after the operation
  90.  *
  91.  * ((left & 0x55555555) << 1) | (right & 0x55555555)
  92.  */
  93. u_char IPLbits[32] = {
  94.      2, 34,  4, 36,  6, 38,  8, 40,
  95.     10, 42, 12, 44, 14, 46, 16, 48,
  96.     18, 50, 20, 52, 22, 54, 24, 56,
  97.     26, 58, 28, 60, 30, 62, 32, 64
  98. };
  99.  
  100.  
  101. /*
  102.  * Bit order after the operation
  103.  *
  104.  * (left & 0xaaaaaaaa) | ((right & 0xaaaaaaaa) >> 1)
  105.  */
  106. u_char IPRbits[32] = {
  107.      1, 33,  3, 35,  5, 37,  7, 39,
  108.      9, 41, 11, 43, 13, 45, 15, 47,
  109.     17, 49, 19, 51, 21, 53, 23, 55,
  110.     25, 57, 27, 59, 29, 61, 31, 63
  111. };
  112.  
  113.  
  114. /*
  115.  * Bit order after the operation
  116.  *
  117.  * ((left & 0x0f0f0f0f) << 4) | (right & 0x0f0f0f0f)
  118.  */
  119. u_char FPLbits[32] = {
  120.      5,  6,  7,  8, 37, 38, 39, 40,
  121.     13, 14, 15, 16, 45, 46, 47, 48,
  122.     21, 22, 23, 24, 53, 54, 55, 56,
  123.     29, 30, 31, 32, 61, 62, 63, 64
  124. };
  125.  
  126.  
  127. /*
  128.  * Bit order after the operation
  129.  *
  130.  * (left & 0xf0f0f0f0) | ((right & 0xf0f0f0f0) >> 4)
  131.  */
  132. u_char FPRbits[32] = {
  133.      1,  2,  3,  4, 33, 34, 35, 36,
  134.      9, 10, 11, 12, 41, 42, 43, 44,
  135.     17, 18, 19, 20, 49, 50, 51, 52,
  136.     25, 26, 27, 28, 57, 58, 59, 60
  137. };
  138.  
  139.  
  140. /*
  141.  * perm - do a permutation with the given table
  142.  */
  143. static void
  144. perm(databits, permtab, leftp, rightp)
  145.     u_char *databits;
  146.     u_char *permtab;
  147.     u_long *leftp;
  148.     u_long *rightp;
  149. {
  150.     register u_long left;
  151.     register u_long right;
  152.     register u_char *PT;
  153.     register u_char *bits;
  154.     register int i;
  155.  
  156.     left = right = 0;
  157.     PT = permtab;
  158.     bits = databits;
  159.  
  160.     for (i = 0; i < 32; i++) {
  161.         left <<= 1;
  162.         if (bits[PT[i]-1])
  163.             left |= 1;
  164.     }
  165.  
  166.     for (i = 32; i < 64; i++) {
  167.         right <<= 1;
  168.         if (bits[PT[i]-1])
  169.             right |= 1;
  170.     }
  171.  
  172.     *leftp = left;
  173.     *rightp = right;
  174. }
  175.  
  176.  
  177. /*
  178.  * doit - make up the tables
  179.  */
  180. static void
  181. doit()
  182. {
  183.     u_char bits[64];
  184.     u_long left;
  185.     u_long right;
  186.     int tabno;
  187.     int i;
  188.     int ind0, ind1, ind2, ind3;
  189.     int quadbits;
  190.  
  191.     memset((char *)bits, 0, sizeof bits);
  192.  
  193.     /*
  194.      * Do the rounds for the IPL table.  We save the results of
  195.      * this as well as printing them.  Note that this is the
  196.      * left-half table.
  197.      */
  198.     printf("static u_long IP[8][16] = {");
  199.     for (tabno = 0; tabno < 8; tabno++) {
  200.         i = tabno * 4;
  201.         ind3 = IPLbits[i] - 1;
  202.         ind2 = IPLbits[i+1] - 1;
  203.         ind1 = IPLbits[i+2] - 1;
  204.         ind0 = IPLbits[i+3] - 1;
  205.         for (quadbits = 0; quadbits < 16; quadbits++) {
  206.             if (quadbits & (1 << 3))
  207.                 bits[ind3] = 1;
  208.             if (quadbits & (1 << 2))
  209.                 bits[ind2] = 1;
  210.             if (quadbits & (1 << 1))
  211.                 bits[ind1] = 1;
  212.             if (quadbits & 1)
  213.                 bits[ind0] = 1;
  214.             perm(bits, IP, &left, &right);
  215.             bits[ind3] = 0;
  216.             bits[ind2] = 0;
  217.             bits[ind1] = 0;
  218.             bits[ind0] = 0;
  219.             if (right != 0) {
  220.                 fprintf(stderr,
  221.                     "IPL tabno %d quad %d right not zero\n",
  222.                     tabno, quadbits);
  223.                 exit(1);
  224.             }
  225.             IPL[tabno][quadbits] = left;
  226.             if (quadbits == 15 && tabno == 7) {
  227.                 printf(" 0x%08x", left);
  228.             } else if (quadbits & 0x3) {
  229.                 printf(" 0x%08x,", left);
  230.             } else {
  231.                 printf("\n\t0x%08x,", left);
  232.             }
  233.         }
  234.         if (tabno == 7)
  235.             printf("\n};\n");
  236.         printf("\n");
  237.     }
  238.  
  239.     /*
  240.      * Compute the right half of the same table.  I noticed this table
  241.      * was the same as the previous one, just by luck, so we don't
  242.      * actually have to do this.  Do it anyway just for a check.
  243.      */
  244.     for (tabno = 0; tabno < 8; tabno++) {
  245.         i = tabno * 4;
  246.         ind3 = IPRbits[i] - 1;
  247.         ind2 = IPRbits[i+1] - 1;
  248.         ind1 = IPRbits[i+2] - 1;
  249.         ind0 = IPRbits[i+3] - 1;
  250.         for (quadbits = 0; quadbits < 16; quadbits++) {
  251.             if (quadbits & (1 << 3))
  252.                 bits[ind3] = 1;
  253.             if (quadbits & (1 << 2))
  254.                 bits[ind2] = 1;
  255.             if (quadbits & (1 << 1))
  256.                 bits[ind1] = 1;
  257.             if (quadbits & 1)
  258.                 bits[ind0] = 1;
  259.             perm(bits, IP, &left, &right);
  260.             bits[ind3] = 0;
  261.             bits[ind2] = 0;
  262.             bits[ind1] = 0;
  263.             bits[ind0] = 0;
  264.             if (left != 0) {
  265.                 fprintf(stderr,
  266.                     "IPR tabno %d quad %d left not zero\n",
  267.                     tabno, quadbits);
  268.                 exit(1);
  269.             }
  270.             if (right != IPL[tabno][quadbits]) {
  271.                 fprintf(stderr,
  272.             "IPR tabno %d quad %d: 0x%08x not same as 0x%08x\n",
  273.                    tabno, quadbits, right,IPL[tabno][quadbits]);
  274.                 exit(1);
  275.             }
  276.         }
  277.     }
  278.  
  279.     /*
  280.      * Next are the FP tables
  281.      */
  282.     printf("static u_long FP[8][16] = {");
  283.     for (tabno = 0; tabno < 8; tabno++) {
  284.         i = tabno * 4;
  285.         ind3 = FPLbits[i] - 1;
  286.         ind2 = FPLbits[i+1] - 1;
  287.         ind1 = FPLbits[i+2] - 1;
  288.         ind0 = FPLbits[i+3] - 1;
  289.         for (quadbits = 0; quadbits < 16; quadbits++) {
  290.             if (quadbits & (1 << 3))
  291.                 bits[ind3] = 1;
  292.             if (quadbits & (1 << 2))
  293.                 bits[ind2] = 1;
  294.             if (quadbits & (1 << 1))
  295.                 bits[ind1] = 1;
  296.             if (quadbits & 1)
  297.                 bits[ind0] = 1;
  298.             perm(bits, FP, &left, &right);
  299.             bits[ind3] = 0;
  300.             bits[ind2] = 0;
  301.             bits[ind1] = 0;
  302.             bits[ind0] = 0;
  303.             if (right != 0) {
  304.                 fprintf(stderr,
  305.                     "FPL tabno %d quad %d right not zero\n",
  306.                     tabno, quadbits);
  307.                 exit(1);
  308.             }
  309.             FPL[tabno][quadbits] = left;
  310.             if (quadbits == 15 && tabno == 7) {
  311.                 printf(" 0x%08x", left);
  312.             } else if (quadbits & 0x3) {
  313.                 printf(" 0x%08x,", left);
  314.             } else {
  315.                 printf("\n\t0x%08x,", left);
  316.             }
  317.         }
  318.         if (tabno == 7)
  319.             printf("\n};");
  320.         printf("\n");
  321.     }
  322.  
  323.     /*
  324.      * Right half of same set of tables.  This was symmetric too.
  325.      * Amazing!
  326.      */
  327.     for (tabno = 0; tabno < 8; tabno++) {
  328.         i = tabno * 4;
  329.         ind3 = FPRbits[i] - 1;
  330.         ind2 = FPRbits[i+1] - 1;
  331.         ind1 = FPRbits[i+2] - 1;
  332.         ind0 = FPRbits[i+3] - 1;
  333.         for (quadbits = 0; quadbits < 16; quadbits++) {
  334.             if (quadbits & (1 << 3))
  335.                 bits[ind3] = 1;
  336.             if (quadbits & (1 << 2))
  337.                 bits[ind2] = 1;
  338.             if (quadbits & (1 << 1))
  339.                 bits[ind1] = 1;
  340.             if (quadbits & 1)
  341.                 bits[ind0] = 1;
  342.             perm(bits, FP, &left, &right);
  343.             bits[ind3] = 0;
  344.             bits[ind2] = 0;
  345.             bits[ind1] = 0;
  346.             bits[ind0] = 0;
  347.             if (left != 0) {
  348.                 fprintf(stderr,
  349.                     "FPR tabno %d quad %d left not zero\n",
  350.                     tabno, quadbits);
  351.                 exit(1);
  352.             }
  353.             if (right != FPL[tabno][quadbits]) {
  354.                 fprintf(stderr,
  355.             "FPR tabno %d quad %d: 0x%08x not same as 0x%08x\n",
  356.                    tabno, quadbits, right,FPL[tabno][quadbits]);
  357.                 exit(1);
  358.             }
  359.         }
  360.     }
  361. }
  362.